import java.util.concurrent.TimeUnit;

public class DeadLock {

    public static void main(String[] args) {
        final Object lockA = new Object();
        final Object lockB = new Object();
        new Thread(() -> {
            synchronized (lockA) {
                System.out.println(Thread.currentThread().getName()+"获得A锁");
                //使A睡眠，保证B先获得B锁
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockB) {
                    System.out.println(Thread.currentThread().getName()+"获得B锁");
                }
            }
        }, "A").start();

        new Thread(() -> {
            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName()+"获得B锁");
                synchronized (lockA) {
                    System.out.println(Thread.currentThread().getName()+"获得A锁");
                }
            }
        }, "B").start();

    }
}